#include "widget.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QtNetwork>
#include <QJsonObject>
#include "networkprocessing.h"
#include "information.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent),
      s_Information(Information::get())
{
    resize(800 , 600);
    QVBoxLayout* total = Initialization();
    setLayout(total);
    clientNetworkProcessing = new NetworkProcessing;
    clientNetworkProcessing->start();
    connect(clientNetworkProcessing, SIGNAL(serverTcpConnectStatic(bool)), this, SLOT(updateTcpSocketView(bool)));
    connect(clientNetworkProcessing, &NetworkProcessing::ClientStatic, this, &Widget::updateClientStaticView);
    connect(&dateTimer, &QTimer::timeout, this, &Widget::updateClientTime);
    connect(&dateTimerHeartBeat, &QTimer::timeout, this, &Widget::dealHeartBeat);//心跳处理

}

Widget::~Widget()
{
}

QVBoxLayout* Widget::Initialization()
{
    // TODO 这里简单布局，之后都可以通过网络或者配置文件修改区域名
    QLabel *clientName = new QLabel(tr("客户端机器"));
    QFont areaFont;
    areaFont.setFamily("楷体");
    areaFont.setBold(true);
    areaFont.setPointSize(36);
    areaFont.setLetterSpacing(QFont::PercentageSpacing,200);
    clientName->setAlignment(Qt::AlignCenter);
    clientName->setFont(areaFont);
    clientName->setStyleSheet("color:#f9a263;");
    clientName->setMaximumHeight(80);
    clientName->setMinimumHeight(80);

    clientStatic = new QLabel(tr("车辆状态:等待指令"));
    clientNetStatic = new QLabel(tr("网络状态:等待连接"));
    QLabel *clientIpaddr = new QLabel(tr("本机IP:") + getHostIp());

    QDateTime currentDateTime =QDateTime::currentDateTime();
    currentDate = currentDateTime.toString("yyyy.MM.dd hh:mm:ss");
    //currentDate = currentDateTime.toString("yyyy.MM.dd hh:mm");
    clientDate = new QLabel(tr("当前时间:") + currentDate);
    dateTimer.start(1000);

    QVBoxLayout *clientInformationLayout = new QVBoxLayout;
    clientInformationLayout->addWidget(clientStatic);
    clientInformationLayout->addWidget(clientNetStatic);
    QGroupBox *clientInformationLayoutBox = new QGroupBox(this);
    clientInformationLayoutBox->setLayout(clientInformationLayout);
    clientInformationLayoutBox->setFont(QFont("楷体", 18, QFont::Bold));

    QHBoxLayout *clientHWInformationLayout = new QHBoxLayout;
    clientHWInformationLayout->addWidget(clientIpaddr);
    clientHWInformationLayout->addWidget(clientDate);
    QGroupBox *clientHWInformationLayoutBox = new QGroupBox(this);
    clientHWInformationLayoutBox->setLayout(clientHWInformationLayout);
    clientHWInformationLayoutBox->setMaximumHeight(60);
    clientHWInformationLayoutBox->setMinimumHeight(60);
    clientHWInformationLayoutBox->setFont(QFont("楷体", 18, QFont::Bold));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(clientName);
    layout->addWidget(clientInformationLayoutBox);
    layout->addWidget(clientHWInformationLayoutBox);

    return layout;
}
QString Widget::getHostIp()
{
    QString IpAddress;
    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    // 获取第一个本主机的IPv4地址
    int nListSize = ipAddressesList.size();
    for (int i = 0; i < nListSize; ++i)
    {
           if ( ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address() )
           {
               IpAddress = ipAddressesList.at(i).toString();
               break;
           }
     }
     // 如果没有找到，则以本地IP地址为IP
     if ( IpAddress.isEmpty() )
        IpAddress = QHostAddress(QHostAddress::LocalHost).toString();
     return IpAddress;
}

void Widget::updateClientTime()
{
    QDateTime currentDateTime =QDateTime::currentDateTime();
    // currentDate = currentDateTime.toString("yyyy.MM.dd hh:mm:ss");
    currentDate = currentDateTime.toString("yyyy.MM.dd hh:mm:ss");
    clientDate->setText(tr("当前时间:") + currentDate);
}

void Widget::updateClientStaticView()
{

    qint16 dataType = 1;
    s_Information.CTS_Data.TruckStart = s_Information.empData.TruckStart;
    s_Information.CTS_Data.DateType = dataType;
    QJsonObject sendJson;

    if(s_Information.empData.TruckStart == 1)
    {
        clientStatic->setText(tr("车辆状态:前进挡启动"));
    }
    if(s_Information.empData.TruckStart == 0)
    {
        clientStatic->setText(tr("车辆状态:车辆停止"));
    }
    sendJson = s_Information.getSendToClientDataPackage();
    clientNetworkProcessing->send(sendJson);
}

void Widget::updateTcpSocketView(bool flag)
{
    if(flag)
    {
        clientNetStatic->setText(tr("网络状态:连接成功"));
        dateTimerHeartBeat.start(15000);

        return;
    }
    clientNetStatic->setText(tr("网络状态:连接断开"));
}

void Widget::dealHeartBeat()
{
    QJsonObject sendJson;
    sendJson.insert("HeartBeat", QString::number(1));
    clientNetworkProcessing->send(sendJson);
}
